package root.action;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import root.service.RegInfo;
import root.service.impl.RegInfoimpl;

/**
 * Servlet implementation class RegDemo
 */
@WebServlet("/regdemo")
public class RegDemo extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegDemo() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
//		数据校验
		if(username==null||username.equals("")||password==null||password.equals("")) {
			request.getRequestDispatcher("error.html").forward(request, response);
			return;
		}
//		左侧接口，特性，功能  ， 右侧的实体，可以自动化完成，不需要程序员写。
		RegInfo service = new RegInfoimpl();
//		boo 插入数据是否成功
		boolean boo = service.insert(username, password);
		if(boo) {
//			返回成功页面
			request.getRequestDispatcher("ok.html").forward(request, response);
		}else {
//			返回失败页面
			request.getRequestDispatcher("userusing.html").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
